// ===================================
// <copyright>
// This file is part of LABSMFController.
//
//    LABSMFController is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    LABSMFController is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with LABSMFController.  If not, see <http://www.gnu.org/licenses/>.
//    Copyright 2011 David Bakker
//
//</copyright>
// <author>David Bakker</author>
// <email>mailto:DavidFBakker@gmail.com</email>
// <created>Wednesday, May 11, 2011</created>
// <lastedit>Wednesday, May 11, 2011</lastedit>
// ===================================

using System;

namespace LABSMFController.LABS.Classses
{
    public static class Settings
    {
        public static Guid SVCGuid = Util.StringToGuid("19d46cc5-74ba-4b10-b737-c76f3c8d30c7");
        public static Guid SVCEventsGuid = Util.StringToGuid("60FFD57E-C3DC-4390-B4C7-3857429FD310");
        public static string[] DNSServers = {"208.67.222.222"};


        public static int PUMPRight = 1;
        public static int PUMPLeft;


        private static Setting[] _tempSettings = new Setting[128];

        private static Setting[] _beerSettings;

        public static int MAXSettings { get; private set; }

        static Settings()
        {
            var a = BeerSettings;
        }
        
        public static Setting[] BeerSettings
        {
            get
            {
                if (_beerSettings == null)
                {
                    //Setting[] gg = SDCard.GetSettings();
                    //if (gg.Length > 1)
                    //{
                    //    MAXSettings = gg.Length;
                    //    BeerSettings = new Setting[MAXSettings];
                    //    BeerSettings = gg;
                    //}
                    //else
                    //{
                        SetupSettings();
                        BeerSettings = new Setting[MAXSettings];
                        for (int c = 0; c < MAXSettings; c++)
                        {
                            BeerSettings[c] = CopySetting(_tempSettings[c]);
                        }
                   // }
                }
                return _beerSettings;
            }
            set { _beerSettings = value; }
        }


        public static string GetSettingsClientOutPut()
        {
            string ret = "HEADER:Settings:Name,Value;";
            for (int c = 0; c < MAXSettings; c++)
            {
                if (BeerSettings[c].Name.Equals("") || BeerSettings[c].Value.Equals("")) continue;

                ret = ret + "\r\n" + BeerSettings[c].Name + "," + BeerSettings[c].Value + ";";
            }
            return ret;
        }


        private static void SetupSettings()
        {
            MAXSettings = 0;
            _tempSettings = new Setting[128];

            const int refreshRecords = 800;
            const int averageCount = 32;
            const double refeshInterval = refreshRecords/averageCount;

            const int bubblerRecordsToKeepInMinutes = 1;

            AddSingleSetting("HeaterRelayDelay", "250");
            AddSingleSetting("DPWSUrl", "urn:uuid:19d46cc5-74ba-4b10-b737-c76f3c8d30c7");
            AddSingleSetting("DPWSEventsUrl", "urn:uuid:" + SVCEventsGuid);
            AddSingleSetting("IPAddress", "192.168.1.91");
            AddSingleSetting("Subnet", "255.255.255.0");
            AddSingleSetting("GateWay", "192.168.1.1");
            AddSingleSetting("Port", "8123");
            AddSingleSetting("Subnet", "255.255.255.0");
            AddSingleSetting("FullSwingSeconds", "90");
            AddSingleSetting("LogToConsole", "1");

            AddSingleSetting("HeaterMinimumLevel", "2");
            AddSingleSetting("RefreshRecords", refreshRecords.ToString());
            AddSingleSetting("RefeshInterval", refeshInterval.ToString());
            AddSingleSetting("AverageCount", averageCount.ToString());
            AddSingleSetting("BubblerRecordsToKeepInMinutes", bubblerRecordsToKeepInMinutes.ToString());

            AddSingleSetting("LoggingSeverity", "0");
            AddSingleSetting("LowSwingTempOffset", "-1");
            AddSingleSetting("HighSwingTempOffset", "-4");

            AddSingleSetting("HLTLevelEmpty", "143");

            AddSingleSetting("MLTLevelEmpty", "174");

            AddSingleSetting("BKLevelEmpty", "190");

            AddSingleSetting("OneGallonFromTapSeconds", "7");
        }

        public static Setting CopySetting(Setting beerSetting)
        {
            var v = new Setting {Name = beerSetting.Name, Value = beerSetting.Value};

            return v;
        }


        private static void AddSingleSetting(string name, string value)
        {
            _tempSettings[MAXSettings] = new Setting
                                            {
                                                Name = name,
                                                Value = value
                                            };

            ++MAXSettings;
        }

        public static string GetSetting(string name)
        {
            for (int c = 0; c < MAXSettings; c++)
            {
                if (BeerSettings[c].Name.Equals(name))
                    return BeerSettings[c].Value;
            }

            return null;
        }
    }

    #region Nested type: Setting

    [Serializable]
    public class Setting
    {
        public string Name;
        public string Value;
    }

    #endregion
}